package com.gonsin.gview.model.table;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.gonsin.gview.model.GButtonModel;
import com.gonsin.gview.model.GFormItemModel;
import com.gonsin.gview.model.i18n.TCloneable;
import com.gonsin.i18n.logic.T;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Data
@ApiModel("每一列的属性")
public class GColumnModel implements TCloneable<GColumnModel> {

    @ApiModelProperty("列标题显示的名字")
    private String name;

    @ApiModelProperty("该列对应的字段名字")
    private String keyName;

    @ApiModelProperty("和keyName作用相同，keyName是直接显示对应的字段，value是动态显示字段值\n" +
            "使用mustache注入，如果value为\"\"时，显示keyName对应的值，如果value非空时显示value的内容\n" +
            "用法例如：\n" +
            "{{item.createTime}}\n" +
            "/files/{{filename}}")
    private String value;

    @ApiModelProperty("宽度占比")
    private int weight = 1;

    @ApiModelProperty("该列显示的内容，分别有 text/date/email/phone/password/qr_code/image/buttons")
    private String columnType;

    @ApiModelProperty("columnType=date时有效，，将日期格式化显示的方式")
    private String format;

    @ApiModelProperty("columnType=buttons时有效，显示按钮")
    private List<GButtonModel> buttons;

    @ApiModelProperty("columnType=buttons时有效，显示按钮")
    private List<GFormItemModel> formItems;

    @ApiModelProperty("columnType=ENUMS时有效，表示枚举对应的值")
    private List<GEnumsModel> enums;

    @Override
    public GColumnModel tClone() {
        GColumnModel model = new GColumnModel();
        BeanUtil.copyProperties(this, model, CopyOptions.create().setIgnoreNullValue(true));
//        model.name = T.t(name);
        if(StringUtils.isNotEmpty(name)){
            model.name = T.t(name);
        }
        if(!CollectionUtils.isEmpty(buttons)){
            model.buttons = new ArrayList<>();
            for(GButtonModel b : buttons){
                model.buttons.add(b.tClone());
            }
        }
        if(!CollectionUtils.isEmpty(formItems)){
            model.formItems = new ArrayList<>();
            for(GFormItemModel b : formItems){
                model.formItems.add(b.tClone());
            }
        }
        if(!CollectionUtils.isEmpty(enums)){
            model.enums = new ArrayList<>();
            for(GEnumsModel b : enums){
                model.enums.add(b.tClone());
            }
        }
        return model;
    }
}
